package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.ISetmealDishService;
import com.itheima.reggie.service.ISetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {

    @Autowired
    private ISetmealDishService setmealDishServic;

    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);


        Long id = setmealDto.getId();
        List<SetmealDish> dishes = setmealDto.getDishes();

        dishes = dishes.stream().map((item) -> {
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());

        setmealDishServic.saveBatch(dishes);

    }

    @Override
    public void removeWithDish(List<Long> ids) {
        //判断套餐是否应该直接删除?处于售卖不可卖
        LambdaQueryWrapper<Setmeal> qw = Wrappers.lambdaQuery(Setmeal.class)
                .eq(Setmeal::getStatus, 1)
                .in(Setmeal::getId, ids);
        int count = this.count(qw);
        if (count >0) {
            //如果不能删除，抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");

        }
        this.removeByIds(ids);
        //删除数据后对应菜品数据也要删除(逻辑删除)
        //涉及到菜品表setmeal_dish
        LambdaQueryWrapper<SetmealDish> qw2 = Wrappers.lambdaQuery(SetmealDish.class)
                .in(SetmealDish::getSetmealId, ids);
        setmealDishServic.remove(qw2);

    }

    @Override
    public SetmealDto selectById(Long id) {

        //传参为setmealDish

        //先查setmeal
        Setmeal setmeal = this.getById(id);

        //创建setmealDto对象
        SetmealDto setmealDto = new SetmealDto();

        //复制到setmealsDish
        BeanUtils.copyProperties(setmeal, setmealDto);

        //查setmealDishes,根据setmeal_id
        LambdaQueryWrapper<SetmealDish> qw = Wrappers.lambdaQuery(SetmealDish.class).eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> dishes = setmealDishServic.list(qw);

        //给setmealDto注入数据
        setmealDto.setDishes(dishes);

        return setmealDto;



    }

    @Override
    public void updateWithDishes(SetmealDto setmealDto) {

        //更改setmeal数据
        this.updateById(setmealDto);

        //更改setmealDish数据,
        // 添加数据,remove+save
        LambdaQueryWrapper<SetmealDish> qw = Wrappers.lambdaQuery(SetmealDish.class).eq(SetmealDish::getSetmealId,setmealDto.getId());

        setmealDishServic.remove(qw);

        List<SetmealDish> dishes = setmealDto.getDishes();
        dishes=dishes.stream().map((item)->{
            item.setDishId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishServic.saveBatch(dishes);

    }


}
